﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;

namespace WMI.NET.CIM
{
    /// <summary>
    /// The CIM_LogicalDevice class represents a hardware entity that may or may not be realized in physical hardware. Logical device characteristics that manage operation or configuration are contained in, or associated with, the CIM_LogicalDevice object. Printer operational properties, for example, are supported paper sizes or detected errors. Sensor device configuration properties, for example, are threshold settings. Various configurations can exist for a logical device and are contained in the CIM_Setting objects, which are associated with the logical device.
    /// </summary>
    public class CIM_LogicalDevice : CIM_LogicalElement
    {
        private Nullable<UInt16> _Availability;
        private Nullable<UInt32> _ConfigManagerErrorCode;
        private Nullable<Boolean> _ConfigManagerUserConfig;
        private string _CreationClassName;
        private string _DeviceID;
        private Nullable<Boolean> _ErrorCleared;
        private string _ErrorDescription;
        private Nullable<UInt32> _LastErrorCode;
        private string _PNPDeviceID;
        private UInt16[] _PowerManagementCapabilities;
        private Nullable<Boolean> _PowerManagementSupported;
        private Nullable<UInt16> _StatusInfo;
        private string _SystemCreationClassName;
        private string _SystemName;


        /// <summary>
        /// Availability and status of the device.
        /// </summary>
        public Nullable<UInt16> Availability { get { return _Availability; } } //TODO Enum

        /// <summary>
        /// Win32 Configuration Manager error code.
        /// </summary>
        public Nullable<UInt32> ConfigManagerErrorCode { get { return _ConfigManagerErrorCode; } } //TODO Enum

        /// <summary>
        /// If TRUE, the device is using a user-defined configuration.
        /// </summary>
        public Nullable<Boolean> ConfigManagerUserConfig { get { return _ConfigManagerUserConfig; } }

        /// <summary>
        /// Name of the class or subclass used in the creation of an instance. When used with other key properties of the class, this property allows all instances of the class and its subclasses to be uniquely identified.
        /// </summary>
        public string CreationClassName { get { return _CreationClassName; } }

        /// <summary>
        /// Address or other identifying information to uniquely name the logical device.
        /// </summary>
        public string DeviceID { get { return _DeviceID; } }

        /// <summary>
        /// If TRUE, the error reported in the LastErrorCode property is now cleared.
        /// </summary>
        public Nullable<Boolean> ErrorCleared { get { return _ErrorCleared; } }

        /// <summary>
        /// Free-form string that supplies information about the error recorded in the LastErrorCode property and corrective actions to perform.
        /// </summary>
        public string ErrorDescription { get { return _ErrorDescription; } }

        /// <summary>
        /// Last error code reported by the logical device.
        /// </summary>
        public Nullable<UInt32> LastErrorCode { get { return _LastErrorCode; } }

        /// <summary>
        /// Indicates the Win32 Plug and Play device identifier of the logical device.
        /// Example: "*PNP030b"
        /// </summary>
        public string PNPDeviceID { get { return _PNPDeviceID; } }

        /// <summary>
        /// Array of the specific power-related capabilities of a logical device.
        /// </summary>
        public UInt16[] PowerManagementCapabilities { get { return _PowerManagementCapabilities; } } //TODO Enum

        /// <summary>
        /// If TRUE, the device can be power managed, that is, put into a power-save state. If FALSE, the integer value 1 ("Not Supported") should be the only entry in the PowerManagementCapabilities array.
        /// This property does not indicate whether power management features are currently enabled, or if enabled, which features are supported. For more information, see the PowerManagementCapabilities array.
        /// </summary>
        public Nullable<Boolean> PowerManagementSupported { get { return _PowerManagementSupported; } }

        /// <summary>
        /// State of the logical device. If this property does not apply to the logical device, the value 5 ("Not Applicable") should be used.
        /// </summary>
        public Nullable<UInt16> StatusInfo { get { return _StatusInfo; } } //TODO Enum

        /// <summary>
        /// Scoping system's creation class name.
        /// </summary>
        public string SystemCreationClassName { get { return _SystemCreationClassName; } }

        /// <summary>
        /// Scoping system's name.
        /// </summary>
        public string SystemName { get { return _SystemName; } }



        /// <summary>
        /// Requests a reset of the logical device. Not implemented by WMI.
        /// </summary>
        /// <returns></returns>
        public UInt32 Reset() { throw new NotImplementedException(); }

        /// <summary>
        /// Defines the desired power state for a logical device and when a device should be put into that state. Not implemented by WMI.
        /// </summary>
        /// <param name="PowerState">A ValueMap value that specifies the desired power state for this logical device.</param>
        /// <param name="Time">Specifies when the power state should be set, either as a regular date-time value or as an interval value (where the interval begins when the method invocation is received). When the PowerState parameter is equal to 5 ("Power Cycle"), the Time parameter indicates when the device should power on again. Power-off is immediate.</param>
        /// <returns></returns>
        public UInt32 SetPowerState(UInt16 PowerState, DateTimeOffset Time) { throw new NotImplementedException(); }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        public CIM_LogicalDevice(ManagementBaseObject mbo)
            : base(mbo)
        {
            _Availability = mbo.GetUshortValue("Availability");
            _ConfigManagerErrorCode = mbo.GetUIntValue("ConfigManagerErrorCode");
            _ConfigManagerUserConfig = mbo.GetBoolValue("ConfigManagerUserConfig");
            _CreationClassName = mbo.GetStringValue("CreationClassName");
            _DeviceID = mbo.GetStringValue("DeviceID");
            _ErrorCleared = mbo.GetBoolValue("ErrorCleared");
            _ErrorDescription = mbo.GetStringValue("ErrorDescription");
            _LastErrorCode = mbo.GetUIntValue("LastErrorCode");
            _PNPDeviceID = mbo.GetStringValue("PNPDeviceID");
            _PowerManagementCapabilities = mbo.GetUshortValueArray("PowerManagementCapabilities");
            _PowerManagementSupported = mbo.GetBoolValue("PowerManagementSupported");
            _StatusInfo = mbo.GetUshortValue("StatusInfo");
            _SystemCreationClassName = mbo.GetStringValue("SystemCreationClassName");
            _SystemName = mbo.GetStringValue("SystemName");

        }
    }
}
